| 1 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  * Copyright (c) 2017-2018 Rafael da Silva Rocha. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Permission is hereby granted, free of charge, to any person obtaining | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * a copy of this software and associated documentation files (the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * "Software"), to deal in the Software without restriction, including | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * without limitation the rights to use, copy, modify, merge, publish, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * distribute, sublicense, and/or sell copies of the Software, and to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * permit persons to whom the Software is furnished to do so, subject to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * the following conditions: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * The above copyright notice and this permission notice shall be | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * included in all copies or substantial portions of the Software. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @fileoverview Function to serialize binary data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @see https://github.com/rochars/byte-data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | import Integer from './integer.js'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | import {validateType} from './validation.js'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | import {unpack as unpackIEEE754, pack as packIEEE754} from './IEEE754.js'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * A class to pack and unpack integers and floating-point numbers. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * @extends {Integer} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | export default class Packer extends Integer { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |    * Set up the object to start serializing/deserializing a data type.. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |    * @param {!Object} theType The type definition. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |    * @throws {Error} If the type definition is not valid. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |    */ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |   setUp(theType) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     validateType(theType); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     super.setUp({ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |       bits: theType.bits, | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |       signed: theType.float ? false : theType.signed}); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     this.setReaderAndWriter_(theType); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |    * Read 1 16-bit float from bytes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |    * @see https://stackoverflow.com/a/8796597 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |    * @param {!Uint8Array|!Array<number>} bytes An array of bytes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |    * @param {number=} i The index to read. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |    * @return {number} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |    * @private | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |   read16F_(bytes, i=0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     return unpackIEEE754(bytes, i, 5, 11); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |    * Read 1 32-bit float from bytes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |    * @param {!Uint8Array|!Array<number>} bytes An array of bytes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |    * @param {number=} i The index to read. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |    * @return {number} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |    * @private | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |   read32F_(bytes, i=0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     return unpackIEEE754(bytes, i, 8, 23); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |    * Read 1 64-bit float from bytes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |    * Thanks https://gist.github.com/kg/2192799 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |    * @param {!Uint8Array|!Array<number>} bytes An array of bytes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |    * @param {number=} i The index to read. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |    * @return {number} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |    * @private | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |   read64F_(bytes, i=0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     return unpackIEEE754(bytes, i, 11, 52); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |    * Write one 16-bit float as a binary value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |    * @param {!Uint8Array|!Array<number>} bytes An array of bytes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |    * @param {number} number The number to write as bytes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |    * @param {number=} j The index being written in the byte buffer. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |    * @return {number} The next index to write on the byte buffer. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |    * @private | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |   write16F_(bytes, number, j=0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     return packIEEE754(bytes, j, number, 5, 11); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |    * Write one 32-bit float as a binary value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |    * @param {!Uint8Array|!Array<number>} bytes An array of bytes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |    * @param {number} number The number to write as bytes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |    * @param {number=} j The index being written in the byte buffer. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |    * @return {number} The next index to write on the byte buffer. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |    * @private | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |   write32F_(bytes, number, j=0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     return packIEEE754(bytes, j, number, 8, 23); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |    * Write one 64-bit float as a binary value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |    * @param {!Uint8Array|!Array<number>} bytes An array of bytes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |    * @param {number} number The number to write as bytes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |    * @param {number=} j The index being written in the byte buffer. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |    * @return {number} The next index to write on the byte buffer. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |    * @private | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |   write64F_(bytes, number, j=0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     return packIEEE754(bytes, j, number, 11, 52); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |    * Set the functions to pack and unpack numbers. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |    * @param {!Object} theType The type definition. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |    * @private | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |   setReaderAndWriter_(theType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     if (theType.float) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |       if (theType.bits == 16) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         this.read = this.read16F_; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         this.write = this.write16F_; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |       } else if(theType.bits == 32) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         this.read = this.read32F_; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         this.write = this.write32F_; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |       } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         this.read = this.read64F_; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         this.write = this.write64F_; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |       } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |       this.read = super.read; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |       this.write = super.write; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |   } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 145 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 146 |  |  |  |